package com.blog.cloud.controller.rec.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.blog.cloud.comm.modal.dto.PageDto;
import com.blog.cloud.comm.modal.dto.ResponeObj;
import com.blog.cloud.comm.modal.dto.TagsDto;
import com.blog.cloud.comm.modal.pojo.BlogContent;
import com.blog.cloud.comm.modal.pojo.Enums;
import com.blog.cloud.comm.modal.pojo.User;
import com.blog.cloud.comm.modal.pojo.UserUploadInf;
import com.blog.cloud.comm.webutils.CommMethod;
import com.blog.cloud.comm.webutils.GetWaterId;
import com.blog.cloud.controller.base.BaseController;
import com.blog.cloud.dao.EnumsDao;
import com.blog.cloud.dao.GalleryTagsDao;
import com.blog.cloud.service.daoservice.UserUploadInfService;
import com.blog.cloud.service.rec.blog.BlogContentService;
import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Page;

public class UserController extends BaseController {
	
	
	
	private BlogContentService  blogContentService = new BlogContentService();
	
	
	private UserUploadInfService userUploadInfService = new UserUploadInfService();
	
	public void index(){
		render(super.getResponeJspPrefix(""));
	}
	
	
	
	/**  
	* @Title: pushBlog  
	* @Description: 发布博客页面
	* @param 
	* @Author LiuFei
	* @Date 2017年12月1日
	*/  
	@ActionKey("user/pushblog")
	public void pushBlog(){
		User user = (User)getSessionAttr("user");
		if(user==null){
			redirect("/");
		}else{
			String waterId = GetWaterId.getWaterId();
			setAttr("serialId", waterId);
			String responeJspPrefix = super.getResponeJspPrefix("rec_user_push_blog.jsp");
			render(responeJspPrefix);
		}
	}
	
	  
	/**  
	* @Title: myBlog  
	* @Description: 我的博客页面
	* @param 
	* @Author LiuFei
	* @Date 2017年12月1日
	*/  
	@ActionKey("user/myblog")
	public void myBlog(){
		User user = (User)getSessionAttr("user");
		if(user==null){
			redirect("/");
		}else{
			String responeJspPrefix = super.getResponeJspPrefix("rec_user_blog_info.jsp");
			render(responeJspPrefix);
		}
	}
	
	
	
	/**  
	* @Title: myBlogPage  
	* @Description: 我的博客文章列表
	* @param 
	* @Author LiuFei
	* @Date 2017年12月1日
	*/  
	@ActionKey("user/myblogpage")
	public void myBlogPage(){
         System.out.println("------OK---------");
         User user = super.getSessionByUser();
         if(user!=null){
        	 PageDto page = new PageDto();
        	 Integer draw = getParaToInt("draw");
        	 Integer start = getParaToInt("start");
        	 Integer length = getParaToInt("length");
        	 String searchVal = getPara("search[value]");
        	 Page<BlogContent> blogPageInit = blogContentService.getBlogPageInit(user.getUserNo(), searchVal, (start/length)+1 , length);
        	 page.setDraw(draw);
        	 page.setRecordsTotal(blogPageInit.getTotalRow());
        	 page.setRecordsFiltered(blogPageInit.getTotalRow());
        	 page.setData(blogPageInit.getList());
        	 renderJson(page);
         }else{
        	 redirect("/");
         }
	}
	
	
	
	
	/**  
	* @Title: submitBlogForm  
	* @Description: 提交博客文章
	* @param 
	* @Author LiuFei
	* @Date 2017年12月1日
	*/  
	
	@ActionKey("user/submitblogform")
	public void submitBlogForm(){
		
		User user = (User)getSessionAttr("user");
		if(user==null){
			redirect("/");
		}else{
			
			ResponeObj resObj = new ResponeObj();
			
			String blogTitle = getPara("blog-title");
			String blogDesc = getPara("blog-desc");
			String blogType = getPara("blog-type");
			String topicTags = getPara("blog-tags");
			String blogContent = getPara("blog-content");
			String blogStatus = getPara("blog-status");
			String serialId = getPara("blog-serial-id");
			blogContentService.setBlogContentVal(user, blogTitle, blogType, blogContent, blogStatus, serialId);
			resObj.setCode("200");
			resObj.setMessage("操作成功！");
			renderJson(resObj);
		}
	}
	
	@ActionKey("user/mygallery")
	public void myGalleryPage(){
		
		
		User user = super.getSessionByUser();
		
		if(user==null){
			render("/");
		}else{
			String pageNumber = getPara("p");
			String tagNo = getPara("tag");
			/*if(org.apache.commons.lang3.StringUtils.isNotEmpty(tagNo)){
				pageNumber = "1";
			}*/
			
			pageNumber = StringUtils.isEmpty(pageNumber)?"1":pageNumber;
			Integer pageSize = 15;
			Enums geetEnumsByKey = EnumsDao.me.geetEnumsByKey("pageSize_Gallery");
			pageSize = Integer.parseInt(geetEnumsByKey.getEvalue());
			//Page<BlogContent> blogPageInit = blogContentService.getBlogPageInit(user.getUserNo(), "", Integer.parseInt(pageSize) , pageNumber);
			Page<UserUploadInf> userUploadInfPage = userUploadInfService.getUserUploadInfPage(user.getUserNo(), tagNo, Integer.parseInt(pageNumber) , pageSize);
			Map<String , List<UserUploadInf>> map = new HashMap<String , List<UserUploadInf>>();
			
			int lie = 3;
			int[] pagePrefix = CommMethod.getPagePrefix(userUploadInfPage.getList().size(), lie);
			if(userUploadInfPage!=null && userUploadInfPage.getList().size()>0){
				List<UserUploadInf> list = userUploadInfPage.getList();
				List<UserUploadInf> reslist =null;
				for (int i = 0; i < lie; i++) {
					int index = pagePrefix[i];
					reslist= new ArrayList<UserUploadInf>();
					for (int j = 0; j < list.size(); j++) {
						
						if(i!=0 && j==0){
							j=pagePrefix[i-1];
						}
						
						if(index<j+1){
							map.put("galleryList_"+i, reslist);
							reslist = null;
							break;
						}else{
							reslist.add(list.get(j));
							if(index==list.size()){
								map.put("galleryList_"+i, reslist);
							}
						}
					}
				}
			}
			String jsonString = JSON.toJSONString(map);
			System.out.println(jsonString);
			
			List<TagsDto> geetGalleryTags = GalleryTagsDao.me.geetGalleryTags();
			setAttr("tags", geetGalleryTags);
			setAttr("map", map);
			
			//userUploadInfPage
			setAttr("p", userUploadInfPage.getPageNumber());
			setAttr("totalPage", userUploadInfPage.getTotalPage());
			setAttr("tagNo", tagNo);
		}
		
		String responeJspPrefix = super.getResponeJspPrefix("rec_user_gallery.jsp");
		render(responeJspPrefix);
	}



	
	
	
	
	
	public void galleryIndex(){
		render(super.getResponeJspPrefix("rec_gallery_index.jsp"));
	}

}
